//
//  MyHttp.m
//  BlockDemo3
//
//  Created by mobao on 16/4/14.
//  Copyright © 2016年 mobao. All rights reserved.
//

#import "MyHttp.h"

//#define REQUESTPUBLICURL @"https://api.weibo.com/2/statuses/public_timeline.json"

//完整路径：BaseUrl + URLString

//请求的主页地址
static NSString * const BaseUrl = @"https://api.weibo.com";

@implementation MyHttp

+(instancetype)sharedClient{
    
    static MyHttp *client = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        client = [[MyHttp alloc]initWithBaseURL:[NSURL URLWithString:BaseUrl]];
        
        client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/json",@"text/javascript",nil];
        
    });

    return client;
}

-(void)networkStateMonitoring:(void(^)(AFNetworkReachabilityStatus status))networkStateMonitoringBlock{
    
    //如果启动就去检测 建议延时调用
    
    //eg:[self performSelector:@selector(login:) withObject:nil afterDelay:0.35f];
    
    //由于检测网络有一定的延迟，所以如果启动app立即去检测调用[AFNetworkReachabilityManager sharedManager].networkReachabilityStatus 有可能得到的是status == AFNetworkReachabilityStatusUnknown;但是此时明明是有网的
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        networkStateMonitoringBlock(status);
        
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}


-(NSURLSessionDataTask*)GET:(NSString*)URLString
                 parameters:(id)parameters
                    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                    failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure
                    showHUD:(BOOL)showHud{
    
    if (showHud) {
        [SVProgressHUD show];
    }
    NSURLSessionDataTask *task = [super GET:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        success(task,responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        failure(task,error);
        
    }];
    return task;
}

-(NSURLSessionDataTask*)POST:(NSString *)URLString
                  parameters:(id)parameters
                     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                     failure:(void (^)(NSURLSessionDataTask * task, NSError *error))failure
                     showHUD:(BOOL)showHud{
    if (showHud) {
        [SVProgressHUD show];
    }
    
    NSURLSessionDataTask *task = [super POST:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (showHud) {
            [SVProgressHUD dismiss];
        }
        
        success(task,responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failure(task,error);
        
    }];
    return task;
}

-(NSURLSessionDataTask*)GET:(NSString*)URLString
                 parameters:(id)parameters
                    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                    failure:(void(^)(NSURLSessionDataTask *task, NSError *error))failure{
    
    NSURLSessionDataTask *task = [self GET:URLString parameters:parameters success:success failure:failure showHUD:YES];
    
    return task;
    
}


-(NSURLSessionDataTask*)POST:(NSString *)URLString
                  parameters:(id)parameters
                     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                     failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{

    NSURLSessionDataTask *task = [self POST:URLString parameters:parameters success:success failure:failure showHUD:YES];
    
    return task;
}


@end
































